package com.hss01248.dialog.bottomsheet;

import com.hss01248.dialog.Tool;
import com.hss01248.dialog.adapter.SuperLvAdapter;
import com.hss01248.dialog.adapter.SuperLvHolder;
import com.hss01248.dialog.adapter.SuperPagerHolder;
import com.hss01248.dialog.config.ConfigBean;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/9.
 */

public class BottomSheetPagerHolder extends SuperPagerHolder<List<BottomSheetBean>> {
    private ListContainer mGridView;
    private SuperLvAdapter adapter;
    private int pageNum;

    public BottomSheetPagerHolder(Context context) {
        super(context);
    }

    public BottomSheetPagerHolder setPageNum(int pageNumber) {
        this.pageNum = pageNumber;
        return this;
    }


    @Override
    protected ComponentContainer setRootView(Context context) {
        mGridView = new ListContainer(context);
        return mGridView;
    }

    public BottomSheetPagerHolder setConfigBean(ConfigBean configBean){
        setGridViewStyle(configBean);
        return this;
    }

    private void setGridViewStyle(final ConfigBean bean) {
        TableLayoutManager tableLayoutManager = new TableLayoutManager();
        tableLayoutManager.setColumnCount(bean.gridColumns);
        mGridView.setLayoutManager(tableLayoutManager);

             adapter = new SuperLvAdapter(bean.context) {
                @Override
                protected SuperLvHolder generateNewHolder(Context context, int itemViewType) {
                    BsGvHolder holder = new BsGvHolder(context);
                    holder.setStyle(bean.bottomSheetStyle);
                    return holder;
                }
            };

             mGridView.setItemClickedListener(new ListContainer.ItemClickedListener() {
                 @Override
                 public void onItemClicked(ListContainer parent, Component view, int position, long id) {
                     if(bean.itemListener !=null){
                         int actualPosition = pageNum * bean.gridColumns * 2 +position;
                         bean.itemListener.onItemClick(bean.lvDatas.get(actualPosition).text,actualPosition);
                     }
                     Tool.dismiss(bean,true);
                 }
             });
        int start = pageNum * bean.gridColumns * 2;
        int end = (pageNum + 1) * bean.gridColumns * 2;
        if (end >= bean.lvDatas.size()) {
            end = bean.lvDatas.size();
        }
        List datas = new ArrayList();
        for(int i = start; i < end; i++ )
        {
            datas.add(bean.lvDatas.get(i));
        }
        adapter.addAll(datas);
        mGridView.setItemProvider(adapter);
    }


    @Override
    protected int setLayoutRes() {
        return 0;
    }

    @Override
    protected void findViews() {

    }

    @Override
    public void assingDatasAndEvents(Context context, List<BottomSheetBean> bean, int position) {
        adapter.refresh(bean);
    }
}
